import { tt } from "../../ttapi_interface/ttapi.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js";
import { EUI_Style } from "./EUI_Style.js";
import { EUI_Window } from "./EUI_Window.js";


export class EUI_Dialog_Folder extends tt2.QUI_Container {
    constructor() {
        super();
        this.localRect.setAsFill();

        //Back
        let back = EUI_Style.CreateWhiteImage();
        back.color = new tt.Color(0, 0, 0, 0.5);
        this.addChild(back);


        this._initTitle();
        {
            var label = EUI_Style.CreateBackWithText("", tt.Color.White);
            this.addChild(label);
            label.localRect.setHPosFill(100, 100);
            label.localRect.setVPosByTopBorder(16, 116);
            this._labelPick = label.getChild(0) as tt2.QUI_Label;
            this._labelPick.cut = true;
            this._labelPick.halign = tt2.QUI_HAlign.Left;

            var panel = EUI_Style.CreateHSplit();
            this.addChild(panel);
            panel.localRect.setHPosFill(100, 100);
            panel.localRect.setVPosFill(132, 100);
            this._panelLeft = panel.getPanel1();
            this._panelRight = new tt2.QUI_Panel_Scroll_Unlimit<string>(this._UpdateItem.bind(this));
            this._panelRight.localRect.setHPosFill();
            this._panelRight.localRect.setVPosFill(0, 16);
            panel.getPanel2().addChild(this._panelRight)


            var btnup = EUI_Style.CreateButton("进入上层目录");
            this.addChild(btnup);
            btnup.localRect.setHPosByRightBorder(160, 380);
            btnup.localRect.setVPosByBottomBorder(16, 100);
            btnup.OnClick = () => {
                let path = tt.PathTool.GetPathName(tt.PathTool.CheckPath(this.PickPath)) + "/";
                this._PickPath(path);
            }

            var btnin = EUI_Style.CreateButton("进入这层目录");
            this.addChild(btnin);
            btnin.localRect.setHPosByRightBorder(160, 200);
            btnin.localRect.setVPosByBottomBorder(16, 100);
            btnin.OnClick = () => {
                this._PickPath(this.PickPath);
            }

            var btn = EUI_Style.CreateButton("打开");
            this.addChild(btn);
            btn.localRect.setHPosByRightBorder(80, 100);
            btn.localRect.setVPosByBottomBorder(16, 100);
            btn.OnClick = () => {
                if (this._win.GetPopTag() == this._tag) {
                    this._isShow = false;
                    this._win.PopModel();
                }
            }
        }

    }
    private _panelLeft: tt2.QUI_Container;
    private _panelRight: tt2.QUI_Panel_Scroll_Unlimit<string>;
    private _labelPick: tt2.QUI_Label;
    private _y: number = 0;
    private _isShow: boolean;
    private _win: EUI_Window

    PickPath: string;
    readonly _tag: string = "_dialog_folder_";
    async Show(win: EUI_Window): Promise<string | null> {
        this._isShow = true;
        this._win = win;
        this._InitFolder();
        win.PushModel(this, this._tag, () => {
            if (this._win.GetPopTag() == this._tag) {
                this._isShow = false;
                this._win.PopModel();
            }
        });

        while (this._isShow) {
            await tt.sleep(1);
        }
        return this.PickPath;
    }


    private _UpdateItem(item: string, elem: tt2.QUI_IElement, id: number, pick: boolean): tt2.QUI_IElement {
        let quiitem = elem as tt2.QUI_Toggle;
        if (quiitem == null) {
            quiitem = EUI_Style.CreateToggle("item");
            quiitem.localRect.setVPosByTopBorder(16);
            quiitem.localRect.setHPosFill();
            quiitem.OnDbClick = () => {
                this._PickPath(this.PickPath);
            }
            quiitem.OnChange = (v) => {
                if (v == true) {
                    let pickid = (quiitem as any).pickid;
                    if (pickid >= 0)
                        this._panelRight.Pick(pickid);
                    this.PickPath = quiitem.Tag;
                    this._labelPick.text = quiitem.Tag;
                }
                if (v == false) {
                    quiitem.value = true;
                }
            }
        }
        (quiitem as any).pickid = id;


        if (item != null) {
            quiitem.Tag = item;
            let path = tt.PathTool.GetFileName(tt.PathTool.CheckPath(item));

            quiitem.value = pick;
            this._UpdateSubLabel(quiitem.ElemTrue, path);
            this._UpdateSubLabel(quiitem.ElemTrueDown, path);
            this._UpdateSubLabel(quiitem.ElemFalse, path);
            this._UpdateSubLabel(quiitem.ElemFalseDown, path);
        }

        return quiitem;
    }
    private _UpdateSubLabel(con: tt2.QUI_IElement, text: string) {
        if (con.getElementType() == tt2.QUI_ElementType.Element_Label) {
            (con as tt2.QUI_Label).text = text;
            (con as tt2.QUI_Label).halign = tt2.QUI_HAlign.Left;
        }
        for (var i = 0; i < con.getChildCount(); i++) {
            let sube = con.getChild(i);
            if (sube.getElementType() == tt2.QUI_ElementType.Element_Label) {
                (sube as tt2.QUI_Label).text = text;
                (sube as tt2.QUI_Label).halign = tt2.QUI_HAlign.Left;
            }
        }
    }

    private _initTitle() {
        let paneltitle = EUI_Style.CreateBackWithText("选择文件夹", tt.Color.White);
        this.addChild(paneltitle);
        paneltitle.localRect.setHPosFill(100, 100);
        paneltitle.localRect.setVPosByTopBorder(16, 100);
        //close btn
        var btn = EUI_Style.CreateCloseButton();
        this.addChild(btn);
        btn.localRect.setHPosByRightBorder(16, 100);
        btn.localRect.setVPosByTopBorder(16, 100);
        btn.OnClick = () => {
            if (this._isShow == true) {
                this._win.PopModel();
                this._isShow = false;
            }
        }
    }


    private async _InitFolder() {
        this._y = 0;
        if (tt2.QExt.extport == 0) {
            let label = EUI_Style.CreateBackWithText("尴尬了，扩展没找到", tt.Color.White);
            label.localRect.setHPosFill();
            label.localRect.setVPosByTopBorder(16, this._y);
            this._y += 16;
            this._panelLeft.addChild(label);
        }
        else {
            let paths = await tt2.QExt_IO.GetRoots();
            for (var key in paths.special) {
                let item = paths.special[key];
                let label = EUI_Style.CreateToggle(key);
                label.Tag = item;
                label.localRect.setHPosFill();
                label.localRect.setVPosByTopBorder(16, this._y);
                this._y += 16;
                this._panelLeft.addChild(label);
                label.OnChange = (v) => {
                    if (v == false) {
                        label.value = true;
                    }
                    this._PickPath(item);
                }
            }
            for (var i = 0; i < paths.logic.length; i++) {
                let item = paths.logic[i];

                let label = EUI_Style.CreateToggle(item);
                label.Tag = item;
                label.localRect.setHPosFill();
                label.localRect.setVPosByTopBorder(16, this._y);
                this._y += 16;
                this._panelLeft.addChild(label);

                label.OnChange = (v) => {
                    if (v == false) {
                        label.value = true;
                    }
                    this._PickPath(item);
                }
            }
        }
        this._panelRight.getItems().splice(0);
    }

    private _PickPath(path: string) {
        this.PickPath = path;
        this._labelPick.text = path;

        //取消不一致的目录
        for (var i = 0; i < this._panelLeft.getChildCount(); i++) {
            let toggle = this._panelLeft.getChild(i) as tt2.QUI_Toggle;
            if (toggle.Tag == null) {
                toggle.value = false;
                continue;
            }
            let left = tt.PathTool.CheckPath(this.PickPath);
            let right = tt.PathTool.CheckPath(toggle.Tag);
            if (left.indexOf(right) != 0) {
                toggle.value = false;
            }
        }
        this._ListFile(path);
    }
    private async _ListFile(path: string) {

        this._panelRight.Clear();
        // this._panelRight.ScrollZero();

        // this._panelRight.getItems().splice(0);
        let list = await tt2.QExt_IO.Dir_List(path, "*.*", false);
        let items = this._panelRight.getItems();
        for (var i = 0; i < list.dirs.length; i++) {
            var name = tt.PathTool.GetFileName(tt.PathTool.CheckPath(list.dirs[i]))
            //$ 开头的是特殊文件夹，没必要管他
            if (name.indexOf("$") == 0 || name.indexOf(".") == 0)
                continue;
            items.push(list.dirs[i]);
        }
    }



}